<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

/**
 * 优雅的图片排版插件，支持短代码快速插入图片组
 * 
 * @package NiceGallery
 * @author Nicek
 * @version 1.0.3
 * @link https://www.n2zip.cn
 */
class NiceGallery_Plugin implements Typecho_Plugin_Interface
{
    public static function activate()
    {
        Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('NiceGallery_Plugin', 'parse');
        Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('NiceGallery_Plugin', 'parse');
        return _t('插件启用成功');
    }

    public static function deactivate()
    {
        return _t('插件禁用成功');
    }

    public static function config(Typecho_Widget_Helper_Form $form)
    {
        $imagesPerRow = new Typecho_Widget_Helper_Form_Element_Text(
            'imagesPerRow',
            null,
            '3',
            _t('每行显示图片数量'),
            _t('请输入每行最多显示的图片数量，默认为3')
        );
        $form->addInput($imagesPerRow);

        $maxImageWidth = new Typecho_Widget_Helper_Form_Element_Text(
            'maxImageWidth',
            null,
            '300',
            _t('图片最大宽度(px)'),
            _t('请输入图片的最大宽度，单位为像素，默认为300')
        );
        $form->addInput($maxImageWidth);

        // 添加插件使用方法
        $usage = new Typecho_Widget_Helper_Form_Element_Textarea(
            'usage',
            null,
            '在编辑器内使用
[NG]
![图片名称1-选填](图片1url)
![图片名称2-选填](图片2url)
[/NG]',
            _t('插件使用方法'),
            _t('在编辑器中使用上述格式插入图片组。')
        );
        $form->addInput($usage);
    }

    public static function personalConfig(Typecho_Widget_Helper_Form $form){}

    public static function parse($content, $widget, $lastResult)
    {
        $content = empty($lastResult) ? $content : $lastResult;
        
        if (false === strpos($content, '[NG]')) {
            return $content;
        }

        $pattern = '/\[NG\](.*?)\[\/NG\]/s';
        return preg_replace_callback($pattern, array('NiceGallery_Plugin', 'parseCallback'), $content);
    }

    public static function parseCallback($matches)
    {
        if (empty($matches[1])) {
            return '';
        }

        // 获取设置
        $options = Helper::options()->plugin('NiceGallery');
        $imagesPerRow = intval($options->imagesPerRow);
        $maxImageWidth = intval($options->maxImageWidth);

        // 提取图片
        $pattern = '/<img.*?src="(.*?)".*?>/i';
        preg_match_all($pattern, $matches[1], $images);

        if (empty($images[1])) {
            return '';
        }

        // 计算样式
        $imageWidth = 100 / $imagesPerRow;
        
        // 构建HTML
        $html = '<div class="nice-gallery" style="display: flex; flex-wrap: wrap; gap: 10px;">';
        foreach ($images[1] as $src) {
            $html .= sprintf(
                '<p class="nice-gallery-item" style="flex: 0 0 calc(%f%% - 10px);">
                    <img src="%s" style="max-width: %dpx; width: 100%%; height: auto; object-fit: cover;">
                </p>',
                $imageWidth,
                $src,
                $maxImageWidth
            );
        }
        $html .= '</div>';

        return $html;
    }
}
